package com.wdzj.commons;

import java.io.*;
import java.util.*;

import jxl.*;
import jxl.format.*;
import jxl.write.*;

public class ExportExcel {

  private Enumeration excel_items; 

  public ExportExcel() {
  }

  public ExportExcel(int i, Vector items) {
   
    if (i == 1) {
      this.excel_items = items.elements();
    }
  }

  //
  public boolean Class_Report_Up(String year,String targetfile, String brandname,
                                 int report_type, String sheetname,
                                 int column, int max_corname,String months,String seasons) {
  
    try {
         jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(
          targetfile)); //生成excel的文件名
      jxl.write.WritableSheet ws = wwb.createSheet("sheet1", 0); //sheet表头
      //设置表头字体的大小
      WritableFont wf_title1 = new WritableFont(WritableFont.TIMES, 13,
                                                WritableFont.BOLD, false,
                                                UnderlineStyle.NO_UNDERLINE,
                                                jxl.format.Colour.BLUE); //设置写入字体
      jxl.write.WritableCellFormat cf_title1 = new jxl.write.WritableCellFormat(
          wf_title1);
      WritableFont wf_title2 = new WritableFont(WritableFont.TIMES, 10,
                                                WritableFont.BOLD, false); 
      jxl.write.WritableCellFormat cf_title2 = new jxl.write.WritableCellFormat(
          wf_title2);
      cf_title2.setAlignment(jxl.format.Alignment.RIGHT);
      //cf_title.setBorder(Border.ALL, BorderLineStyle.THIN);
      //cf_title.setBackground(jxl.format.Colour.YELLOW);
      //cf_title.setAlignment(jxl.format.Alignment.CENTRE);
     
      WritableFont wf = new WritableFont(WritableFont.TIMES, 10,
                                         WritableFont.BOLD, false); 
      jxl.write.WritableCellFormat cf = new jxl.write.WritableCellFormat(wf);
      //cf.set
      //cf.setBorder(Border.ALL, BorderLineStyle.THIN);
      //cf.setBackground(jxl.format.Colour.YELLOW);
      cf.setAlignment(jxl.format.Alignment.JUSTIFY);

      jxl.write.WritableCellFormat cf1 = new jxl.write.WritableCellFormat(wf);
      cf1.setAlignment(jxl.format.Alignment.CENTRE);
      
       WritableFont wf2 = new WritableFont(WritableFont.TIMES, 10,
              WritableFont.BOLD, false,UnderlineStyle.NO_UNDERLINE,
              jxl.format.Colour.RED); 
       jxl.write.WritableCellFormat cf2 = new jxl.write.WritableCellFormat(wf2);
       cf2.setAlignment(jxl.format.Alignment.CENTRE);
      
       int k;
     
      ws.setColumnView(0, 14);
      ws.setColumnView(1, 20);
      ws.setColumnView(2, (max_corname + 1) * 2);
      //for (k = 0; k < column + 1; k++) {
      //ws.setColumnView(k + 1, max_item*2);
      //}

      jxl.write.Label labelC;
      ArrayList excelitem = new ArrayList();
      int no = 0;
      int row_no = 0; //行号

      String remark = "";
     
      labelC = new jxl.write.Label(0, 0, "填表说明:", cf_title2);
      ws.addCell(labelC);
      labelC = new jxl.write.Label(1, 0, remark, cf);
      ws.addCell(labelC);

      //表第二行：写表名称
      labelC = new jxl.write.Label(0, 1, "表名:", cf_title2);
      ws.addCell(labelC);
      

      while (this.excel_items.hasMoreElements()) {
    	 
        no = no + 1;
        // System.out.println("no***"+no);
        excelitem = (ArrayList)this.excel_items.nextElement();

if (no == 1) { //写表头
         
          for (k = 0; k < excelitem.size(); k++) {
            labelC = new jxl.write.Label(0, k + row_no + 4, (String)excelitem.get(k) , cf);
            ws.addCell(labelC);
               }
        }
        else if (no == 2) { //д��ͷ
          //д�б���,���ֹ�˾�Ļ�����
          for (k = 0; k < excelitem.size(); k++) {
            labelC = new jxl.write.Label(1, k + row_no + 4, (String)excelitem.get(k).toString() , cf);
            ws.addCell(labelC);
            // System.out.println("������"+(String)excelitem.get(k));
          }
        }
        else if (no == 3) { //д��ͷ
          //д�б���,���ֹ�˾�����
          for (k = 0; k < excelitem.size(); k++) {
            labelC = new jxl.write.Label(2, k + row_no + 4,
                                         excelitem.get(k).toString(), cf);
           
            ws.addCell(labelC);
            // System.out.println("�ֹ�˾�����"+(String)excelitem.get(k));
          }
        }

        else if (no == 4) {

          //д�б���,���������Ŀ
          for (k = 0; k < excelitem.size(); k++) {
            //�����б����п�
            ws.setColumnView(k + 3,
                             (excelitem.get(k).toString().trim().length() + 1) *
                             2);
            labelC = new jxl.write.Label(k + 3, row_no + 2,
                                         excelitem.get(k).toString(), cf);
            ws.addCell(labelC);
            // System.out.println("�������Ŀ"+excelitem.get(k).toString());
          }
        }
        else {
          //д������Ŀ�����λ
          for (k = 0; k < excelitem.size(); k++) {
        	 if(((String)excelitem.get(k)).equals("")){
        		 labelC = new jxl.write.Label(k + 3, row_no + 3,"������д",cf2); 
        	 }
            //�����б����п�
        	 else{ 
        	 labelC = new jxl.write.Label(k + 3, row_no + 3,
                                         excelitem.get(k).toString(), cf1);
        	 }
            ws.addCell(labelC);
            // System.out.println("���λ"+excelitem.get(k).toString());
          }

        }
       
      }
      //�ϲ���ͷ����ĵ�Ԫ��
      ws.mergeCells(1, 0, column + 2, 0);
      ws.mergeCells(1, 1, column + 2, 1);
      ws.mergeCells(1, 2, column + 2, 2);
      if (report_type==1)
      {
        ws.mergeCells(0, 3, column + 2, 3);
        ws.mergeCells(0, 4, 0, 5);
        ws.mergeCells(1, 4, 1, 5);
        ws.mergeCells(2, 4, 2, 5);
      }
      else
      {
        ws.mergeCells(1, 3, column + 2, 3);
        ws.mergeCells(0, 4, column + 2, 4);
        ws.mergeCells(0, 5, 0, 6);
        ws.mergeCells(1, 5, 1, 6);
        ws.mergeCells(2, 5, 2, 6);
      }
      wwb.write(); //�ر�Excel����������
      wwb.close();

    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return true;
  }

}
